package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dao.CategoryDao;
import com.itheima.entity.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CategoryServiceImpl  implements  CategoryService {

    @Autowired
    private CategoryDao categoryDao;


    @Override
    public IPage<Category> getCategory(Integer page, Integer pageSize) {
        IPage<Category> categoryIPage = new Page<>();
        categoryIPage.setPages(page);
        categoryIPage.setSize(pageSize);

        return  categoryDao.selectPage(categoryIPage,null);
    }

    @Override
    public Integer addaddCategory(Category category) {


        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getName,category.getName());
        Category resultCategory = categoryDao.selectOne(lqw);
        System.out.println(resultCategory);
        if (resultCategory !=null){
         return null;
        }
     return categoryDao.insert(category);
    }

    @Override
    public Integer updateCategory(Category category) {

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Category::getId,category.getId());

        return categoryDao.update(category, lqw);
    }

    @Override
    public Integer deleteCategory(Long id) {


        return categoryDao.deleteById(id);
    }

    @Override
    public List<Category> getByTypeCategory(Integer type) {

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type != null ,Category::getType,type);

        return   categoryDao.selectList(lqw);
    }
}
